package com.ldk.admin.factory;

import java.util.ArrayList;
import java.util.List;

import com.ldk.framework.entity.Region;

public class RegionFactory {
	
	/**
	 * 菜单树建立器
	 * @param menus
	 * @return
	 */
	public List<RegionExt> build(List<Region> regions)
	{
		if(regions == null) return null;
		List<RegionExt> regionsExt = new ArrayList<RegionExt>();
		for(Region region: regions){
			//根菜单
			if(region.getParentId() == 0){
				RegionExt regionExt = new RegionExt();
				regionExt.setRegionId(region.getRegionId());
				regionExt.setParentId(region.getParentId());
				regionExt.setRegionName(region.getRegionName());
				regionExt.setRegionType(region.getRegionType());
				regionExt.setFirstLetter(region.getFirstLetter());
				regionExt.setEnabled(region.getEnabled());
				
				List<RegionExt> children = buildChild(region, regions);
				regionExt.setChildren(children);
				
				regionsExt.add(regionExt);
			}
		}
		return regionsExt;
	}
	
	private List<RegionExt> buildChild(Region parent, List<Region> regions){
		if(parent == null || regions == null) return null;
		List<RegionExt> regionsExt = new ArrayList<RegionExt>();
		for(Region region:regions){
			if(region.getParentId() == parent.getRegionId()){
				RegionExt regionExt = new RegionExt();
				regionExt.setRegionId(region.getRegionId());
				regionExt.setParentId(region.getParentId());
				regionExt.setRegionName(region.getRegionName());
				regionExt.setRegionType(region.getRegionType());
				regionExt.setFirstLetter(region.getFirstLetter());
				regionExt.setEnabled(region.getEnabled());
				
				List<RegionExt> children = buildChild(region, regions);
				regionExt.setChildren(children);
				regionsExt.add(regionExt);
				
				if(children == null) continue;
			}
		}
		return regionsExt;
	}
}
